package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import modelo.Ubigeo;
import util.ConexionBD;
import excepcion.*;

public class UbigeoDAO extends BaseDAO  {

	public Collection<Ubigeo> listarDistritos(String idDepartamento, String idProvincia ) throws DAOExcepcion {
		System.out.println("UbigeoDAO: listar()");
		Collection<Ubigeo> c = new ArrayList<Ubigeo>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			String query = "select idDistrito,distrito from ubigeo where idDepartamento = ? and idProvincia = ? ";
			stmt = con.prepareStatement(query);
			stmt.setString(1, idDepartamento);
			stmt.setString(2, idProvincia);
			rs = stmt.executeQuery();
			while (rs.next()) {
				Ubigeo vo = new Ubigeo();
				vo.setIddistrito(rs.getString("idDistrito"));
				vo.setDistrito((rs.getString("distrito")));
				c.add(vo);
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return c;
	}

}
